package view;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.LayoutManager;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import modelo.Hora;
import modelo.Minutos;

public class ImagePanel extends JPanel
{
/*the default image to use*/
String imageFile = "clock.jpg";

private Hora miHora;
private Minutos misMinutos;

public ImagePanel()
{
super();

}

public ImagePanel(String image)
{
super();
this.imageFile = image;
}

public ImagePanel(LayoutManager layout,Hora h,Minutos m)
{
super(layout);
miHora = h;
misMinutos = m;
}

public void paintComponent(Graphics g)
{
/*create image icon to get image*/
ImageIcon imageicon = new ImageIcon(getClass().getResource(imageFile));
Image image = imageicon.getImage();

/*Draw image on the panel*/
super.paintComponent(g);

if (image != null)
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
paintTiempo(g);
}

public void paintTiempo(Graphics g){
		Dimension d = this.getSize();
		int[] xPoints = new int[3];
		int[] yPoints = new int[3];
		
		
		int dx = 8;
		double ang = ((90*2*Math.PI)/360);
		
		int x0 = (int) Math.round(miHora.getX1() - (Math.sin(ang - miHora.getAngulo())*dx));
		int x1 = (int) Math.round(miHora.getX1() + (Math.sin(ang - miHora.getAngulo())*dx));
		int y0 = (int) Math.round(miHora.getY1() + (Math.cos(ang - miHora.getAngulo())*dx));
		int y1 = (int) Math.round(miHora.getY1() - (Math.cos(ang - miHora.getAngulo())*dx));
		
		xPoints[0] = x0;
		xPoints[1] = x1;
		xPoints[2] = miHora.getX2();
		yPoints[0] = d.height-y0;
		yPoints[1] = d.height-y1;
		yPoints[2] = d.height-miHora.getY2();
		//g.drawImage(horaImage, x0, y0, x1, y1, x0 + (int) Math.round(miHora.getLength()*Math.sin(miHora.getAngulo())), (int) Math.round(y0 + miHora.getLength()*Math.cos(miHora.getAngulo())), (int) Math.round(x1 + miHora.getLength()*Math.sin(miHora.getAngulo())),  (int) Math.round(y1 + miHora.getLength()*Math.cos(miHora.getAngulo())), null);
		g.fillPolygon(xPoints, yPoints, 3);
		
		
		int[] xMinPoints = new int[3];
		int[] yMinPoints = new int[3];
		
		int xMin0 = (int) Math.round(misMinutos.getX1() - (Math.sin(ang - misMinutos.getAngulo())*dx));
		int xMin1 = (int) Math.round(misMinutos.getX1() + (Math.sin(ang - misMinutos.getAngulo())*dx));
		int yMin0 = (int) Math.round(misMinutos.getY1() + (Math.cos(ang - misMinutos.getAngulo())*dx));
		int yMin1 = (int) Math.round(misMinutos.getY1() - (Math.cos(ang - misMinutos.getAngulo())*dx));
		
		xMinPoints[0] = xMin0;
		xMinPoints[1] = xMin1;
		xMinPoints[2] = misMinutos.getX2();
		yMinPoints[0] = d.height-yMin0;
		yMinPoints[1] = d.height-yMin1;
		yMinPoints[2] = d.height-misMinutos.getY2();
		g.fillPolygon(xMinPoints, yMinPoints, 3);
	}

    void setHoraMinutos(Hora miHora, Minutos misMinutos) {
        this.miHora = miHora;
        this.misMinutos = misMinutos;
    }

}